const PROJECTS_ID= '1883442361'
const APP_ID= '602394140839161856'
const APP_KEY= 'e027565ee9954929a41c152deecdf1c1'
const APPLICATION_NAME= 'idls'
/**
 * url:请求的url
 * params:请求参数
 * message:loading提示信息
 * success:成功的回调
 * fail:失败的回调
 */
const util = require('chiship-common-util')

//post请求
function postRequest(url, params, success, fail) {
  this.postRequestLoading(url, params, "", success, fail)
}

//根据判断message 是否显示loading
function postRequestLoading(url, params, message, success, fail) {
  if (message != "") {
    wx.showLoading({
      title: message,
    })
  }
  const postRequestTask = wx.request({
    url: url,
    data: params,
    header: {
      'content-type': 'application/json;charset=UTF-8',
      "ProjectsId":PROJECTS_ID,
      "App-Id":APP_ID,
      "App-Key":APP_KEY,
      "Application-Name":APPLICATION_NAME,
      "Sign": 'MD5=123456',
      "Access-Token":wx.getStorageSync('Token')
    },//
    method: 'POST',
    success: function (res) {
      if (message != "") {
        wx.hideLoading()
      }
      if (res.statusCode == 200 && res.data.code == 200) {
        success(res.data.data);
      } else if (res.data.code == 502 || res.data.code == 506003) {
        wx.showToast({
          title: res.data.message || '',
          icon: 'none',
          duration: 2000,
          mask: true
        })
        setTimeout(function(){
          wx.clearStorageSync('userInfo')
          wx.reLaunch({
            url: '/pages/login/index'
          })
        },1000)
      } else {
        wx.showToast({
          title: res.data == null ? res.data.message : typeof (res.data) == 'object' ? res.data.message : res.data,
          icon: 'none',
          duration: 3000,
          mask: true
        })
        fail(res)
      }
    },
    fail: function (res) {
      if (message != "") {
        wx.hideLoading()
      }
      wx.showToast({
        title: res.errMsg || '网络故障',
        icon: 'none',
        duration: 2000,
        mask: true
      })
      fail(res)
    }
  })
}

//get请求
function getRequest(url, params, success, fail) {
  this.getRequestLoading(url, params, "加载中", success, fail,true)
}

function getRequestLoading(url, params, message, success, fail,errorShow) {
  if (message != "") {
    wx.showLoading({
      title: message,
    })
  }
  const getRequestTask = wx.request({
    url: url,
    data: params,
    header: {
      'content-type': 'application/json;charset=UTF-8',
      "ProjectsId":PROJECTS_ID,
      "App-Id":APP_ID,
      "App-Key":APP_KEY,
      "Application-Name":APPLICATION_NAME,
      "Sign": 'MD5=123456',
      "Access-Token":wx.getStorageSync('Token')
    },
    method: 'GET',
    success: function (res) {
      if (message != "") {
        wx.hideLoading()
      }
      if (res.statusCode == 200 && res.data.code == 200) {
        success(res.data.data)
      } else if (res.data.code == 502 || res.data.code == 506003) {
        wx.showToast({
          title: res.data.message || '',
          icon: 'none',
          duration: 2000,
          mask: true
        })
        setTimeout(function(){
          wx.clearStorageSync('userInfo')
          wx.reLaunch({
            url: '/pages/login/index'
          })
        },1000)
      } else {
        if(errorShow){
          wx.showToast({
            // title: res.data.data == null ? res.data.message : typeof (res.data.data) == 'object' ? JSON.stringify(res.data.data) : res.data.data,
            title: res.data == null ? res.data.message : typeof (res.data) == 'object' ? res.data.message : res.data,
            icon: 'none',
            duration: 3000,
            mask: true
          })
        }
        fail(res.data)
      }
    },
    fail: function (res) {
      if (message != "") {
        wx.hideLoading()
      }
      wx.showToast({
        title: res.errMsg || '网络故障',
        icon: 'none',
        duration: 2000,
        mask: true
      })
      fail(res)
    }
  })
}

//取消post请求
function abortPostRequest(url, params, success, fail) {
  postRequestTask.abort()
}

//取消get请求
function abortGetRequest(url, params, success, fail) {
  getRequestTask.abort()
}

//上传文件请求post
function postUploadFileLoading(url, params, message, success, fail) {
  if (message != "") {
    wx.showLoading({
      title: message,
    })
  }
  let encrypt = util.setEncrypt(params,"05d2a12");
  const postUploadFileTask = wx.uploadFile({
    url: url,
    filePath: params.tempFilePath,
    name:"file",//后台要绑定的名称
    header: {
      "Content-Type": "multipart/form-data",
      "Access-Token": wx.getStorageSync('accessToken'),
      "App-ID":"70988c04c4754a9",
      "APP-KEY":"093941aa4e36456a906cdd5123dec51b",
      "Application-Name":" subjectMiniProgram",
      "Sign": 'MD5=123456',
      "Encrypt": encrypt
    },
    //参数绑定
    formData:{
      catalogId: params.catalogId,
      fileName: params.fileName
    },
    success:function(res){
      if (message != "") {
        wx.hideLoading()
      }
      res.data = JSON.parse(res.data)
      if (res.statusCode == 200 && res.data.code == 200) {
        success(res.data.data);
      } else if (res.data.code == 502 || res.data.code == 506003) {
        wx.showToast({
          title: '502||506003',
          icon: 'none',
          duration: 2000,
          mask: true
        })
        setTimeout(function () {
          wx.navigateTo({
            url: '/pages/login/index'
          })
        }, 1000)
      } else {
        wx.showToast({
          title: 'error',
          icon: 'none',
          duration: 2000,
          mask: true
        })
        fail(res)
      }
    },
    fail: function(ress){
      if (message != "") {
        wx.hideLoading()
      }
      wx.showToast({
        title: '网络故障',
        icon: 'none',
        duration: 2000,
        mask: true
      })
      fail(res)
    }
  })
}
module.exports = {
  postRequest: postRequest,
  postRequestLoading: postRequestLoading,
  getRequest: getRequest,
  getRequestLoading: getRequestLoading,
  abortPostRequest: abortPostRequest,
  abortGetRequest: abortGetRequest,
  postUploadFileLoading: postUploadFileLoading
}